import  * as PersonalCertificationService from 'services/PersonalCertificationService';
import {  Toast } from 'antd-mobile';
export default {
  namespace: 'PersonalCertificationModel',
  state: {

  },
  effects: {
    *postThirdPhone({ payload, callback }, { call, put }) {
      localStorage.setItem('Captcha',payload.verificationCode)
      localStorage.setItem('Contact',payload.phone)
      /**
       * 提交用户填写的信息
       */
      const response = yield call(PersonalCertificationService.PostThirdVerifications, payload);
      let reg = new RegExp(/0000$/, 'i')
      let { code,captcha } = response
      //如果操作成功
      if(reg.test(code)){
        Toast.hide()
        let { identity: { status } } = response
        yield put({
          type: 'setData',
          payload: status
        });
        if(status === 'PASSED') {
          localStorage.setItem('Captcha',captcha)
          yield callback('success')
        } else {
          yield callback('error')
        }
      }else {
        if(code==='100100170'||code==='100100165'){
          Toast.fail('验证码错误！')
        }else {
          yield callback('error')
        }
      }

    },
    *getVerificationCode({ payload, callback }, { call, put }) {
      const response = yield call(PersonalCertificationService.getVerificationCode, payload);

      yield callback(response)
    }
  },
  reducers: {
    setData(state, { payload }) {
      return {
        ...state,
        list: payload,
      }
    }
  }
};
